﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace TimeQuest.Base
{
    public class GameWorld
    {
        #region Constructors

        public GameWorld()
        {
            Screens = new List<Screen>();
        }

        #endregion

        #region Properties

        public static bool ShowBlockingRectangles { get; set; }
        public Player Player { get; set; }
        public IList<Screen> Screens { get; set; }
        public Screen CurrentScreen { get; set; }

        #endregion

        public void LoadWorld(int bgTextureID, int treetextureid, int rocktextureid, int itemTextureID, int gameObjectsTextureID)
        {
            Screen s2 = new Screen();
            s2.BackGroundTextureID = bgTextureID;
            s2.TextureOffset = new Point(1, 0);

            InvisibleWall top2 = new InvisibleWall();
            top2.GameObjectID = Guid.NewGuid();
            top2.SetBaseRectangle(new Rectangle(400, 0, 640, 310));

            s2.GameObjects.Add(top2);

            Screen s1 = new Screen();
            s1.BackGroundTextureID = bgTextureID;
            s1.ScreenID = Guid.NewGuid();
            s1.TextureOffset = new Point(0, 0);
            s1.LeftScreen = s2;
            s2.RightScreen = s1;

            GameObject o1 = new GameObject();
            o1.GameObjectTextureID = treetextureid;
            o1.Location = new Microsoft.Xna.Framework.Point(650, 250);
            o1.BaseHeight = 35;
            o1.BaseWidth = 75;
            o1.BaseYoffset = 240;
            o1.BaseXoffset = 10;
            o1.GameObjectID = Guid.NewGuid();

            GameObject o2 = new GameObject();
            o2.GameObjectTextureID = rocktextureid;
            o2.Location = new Microsoft.Xna.Framework.Point(170, 300);
            o2.BaseWidth = 100;
            o2.BaseHeight = 30;
            o2.BaseYoffset = 60;
            o2.GameObjectID = Guid.NewGuid();

            InvisibleWall RiverBottom = new InvisibleWall();
            RiverBottom.GameObjectID = Guid.NewGuid();
            RiverBottom.SetBaseRectangle(new Rectangle(275, 475, 230, 100));

            InvisibleWall RiverBottom2 = new InvisibleWall();
            RiverBottom2.GameObjectID = Guid.NewGuid();
            RiverBottom2.SetBaseRectangle(new Rectangle(245, 535, 290, 100));

            InvisibleWall RiverTop = new InvisibleWall();
            RiverTop.GameObjectID = Guid.NewGuid();
            RiverTop.SetBaseRectangle(new Rectangle(325, 315, 150, 120));

            InvisibleWall top = new InvisibleWall();
            top.GameObjectID = Guid.NewGuid();
            top.SetBaseRectangle(new Rectangle(0, 0, 640, 310));

            //InvisibleWall BottomBorder = new InvisibleWall();
            //BottomBorder.GameObjectID = Guid.NewGuid();
            //BottomBorder.SetBaseRectangle(new Rectangle(0, 480, 640, 1));

            //InvisibleWall RightBorder = new InvisibleWall();
            //RightBorder.GameObjectID = Guid.NewGuid();
            //RightBorder.SetBaseRectangle(new Rectangle(640, 0, 1, 480));

            //InvisibleWall LeftBorder = new InvisibleWall();
            //LeftBorder.GameObjectID = Guid.NewGuid();
            //LeftBorder.SetBaseRectangle(new Rectangle(0, 0, 1, 480));

            s1.GameObjects.Add(o1);
            s1.GameObjects.Add(o2);
            s1.GameObjects.Add(RiverBottom);
            s1.GameObjects.Add(RiverBottom2);
            s1.GameObjects.Add(RiverTop);
            s1.GameObjects.Add(top);
            //s1.GameObjects.Add(BottomBorder);
            //s1.GameObjects.Add(RightBorder);
            //s1.GameObjects.Add(LeftBorder);

            StaticPictureItem i = new StaticPictureItem();
            i.GameObjectTextureID = itemTextureID;
            i.MapImageRectangle = new Rectangle(1, 1, 10, 10);
            i.Location = new Point(500, 400);
            s1.GameObjects.Add(i);

            s1.GameObjects.Add(AnimatedPictureItem.BuildTest(itemTextureID));
            s1.GameObjects.Add(AnimatedPicture.BuildTest(gameObjectsTextureID));

            Screens.Add(s1);
            CurrentScreen = Screens.First();
        }

        public void Draw(SpriteBatch sb)
        {
            var beforePlayer = CurrentScreen.GameObjects.Where(x => x.CenterOfBaseY < Player.CenterOfBaseY).ToList();
            var afterPlayer = CurrentScreen.GameObjects.Where(x => x.CenterOfBaseY >= Player.CenterOfBaseY).ToList();

            CurrentScreen.Draw(sb);
            foreach (GameObject o in beforePlayer)
            {
                o.Draw(sb);
            }
            Player.Draw(sb);
            foreach (GameObject o in afterPlayer)
            {
                o.Draw(sb);
            }
        }
    }
}